package com.ykdl.member.kid.adapters;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ykdl.member.kid.R;
import com.ykdl.member.KidApp;
import com.ykdl.member.dictionary.DictionaryDataManager;
import com.ykdl.member.kid.beans.BabyProfileBean;
import com.ykdl.member.kid.beans.BreastFeeding;
import com.ykdl.member.kid.beans.Feeding;
import com.ykdl.member.kid.beans.Nappy;
import com.ykdl.member.kid.beans.Nursing;
import com.ykdl.member.kid.beans.Sleeping;
import com.ykdl.member.kid.util.AssertTool;
import com.ykdl.member.kid.util.DateUtil;
/**
 * 身高，体重，头围数据
 * @author Pengfei
 *
 */
public class FeedAdapter extends BaseAdapter {
	
	private static final int TYPE_VIEW_DIAPER = 1;
	private static final int TYPE_VIEW_SLEEP = 2;
	private static final int TYPE_VIEW_FEEDER = 3;
	private static final int TYPE_VIEW_BREAST = 4;
	
	private Context context;
	private List<Nursing> beans = new ArrayList<Nursing>();
	private BabyProfileBean baby;
	private SimpleDateFormat format = new SimpleDateFormat("HH:mm");
	
	public FeedAdapter(Context context){
		this.context = context;
	}
	
	public void setBaby(BabyProfileBean baby) {
		this.baby = baby;
	}

	public List<Nursing> getBeans() {
		return beans;
	}

	public void addBeans(List<Nursing> beans) {
		for (Nursing b : beans) {
			this.beans.add(b);
		}
	}
	
	public void cleanData(){
		this.beans.clear();
	}

	@Override
	public int getCount() {
		if (beans == null) {
			return 0;
		} else {
			return beans.size();
		}
	}
	
//	@Override
//	public int getViewTypeCount() {
//		return 4;
//	}
//	
//	@Override
//	public int getItemViewType(int position) {
//		if (getItem(position).getNursing_type() == Nursing.NURSING_RECORD_TYPE_NAPPY) {
//			return TYPE_VIEW_DIAPER;
//		} else if (getItem(position).getNursing_type() == Nursing.NURSING_RECORD_TYPE_SLEEPING) {
//			return TYPE_VIEW_SLEEP;
//		} else if (getItem(position).getNursing_type() == Nursing.NURSING_RECORD_TYPE_FEEDING) {
//			return TYPE_VIEW_FEEDER;
//		} else if (getItem(position).getNursing_type() == Nursing.NURSING_RECORD_TYPE_BREAST_FEEDING) {
//			return TYPE_VIEW_BREAST;
//		} else {
//			return 0;
//		}
//	}

	@Override
	public Nursing getItem(int position) {
		return beans.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_feed_diaper, null);
			holder = new ViewHolder();
			holder.imgView = (ImageView) convertView.findViewById(R.id.imgView);
			holder.txtValue = (TextView) convertView.findViewById(R.id.txtValue);
			holder.txtAge = (TextView) convertView.findViewById(R.id.txtAge);
			holder.txtDate = (TextView) convertView.findViewById(R.id.txtDate);
					
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// TODO 设置数据
		Nursing nur = getItem(position);
		if (nur instanceof Nappy) {
			Nappy na = (Nappy) nur;
			holder.imgView.setImageResource(R.drawable.icon_diaper);
			String excrete_type = DictionaryDataManager.getInstance().getValueById(context, AssertTool.NAPPY, na.getExcrete_type());
			String spill_over = na.getSpill_over() == 1 ? "有溢出":"无溢出";
			String shape = DictionaryDataManager.getInstance().getValueById(context, AssertTool.DEFECATE_SHAPE, na.getShape());
			String color = DictionaryDataManager.getInstance().getValueById(context, AssertTool.DEFECATE_COLOR, na.getColor());
			Date d = new Date((long) na.getCreate_time() * 1000);
			holder.txtValue.setText(format.format(d)+" "+excrete_type+" "+spill_over+" "+(TextUtils.isEmpty(shape)?"":shape)+" "+(TextUtils.isEmpty(color)?"":color));
			long time = (long) baby.getBirthday() * 1000;
			String age = DateUtil.getBabyAge(time, (long) na.getCreate_time());
			holder.txtAge.setText(age);
			holder.txtDate.setText(DateUtil.getDateData(na.getCreate_time()));
		} else if (nur instanceof Sleeping) {
			Sleeping slp = (Sleeping) nur;
			holder.imgView.setImageResource(R.drawable.icon_sleep);
			Date d = new Date((long) (slp.getBegin_time()*1000));
			
			holder.txtValue.setText(format.format(d)+" 入睡   时长"+ formatTime(slp.getDuration()));
			long time = (long) baby.getBirthday() * 1000;
			String age = DateUtil.getBabyAge(time, (long)slp.getCreate_time());
			holder.txtAge.setText(age);
			holder.txtDate.setText(DateUtil.getDateData(slp.getCreate_time()));
		} else if (nur instanceof Feeding) {
			Feeding fd = (Feeding) nur;
			holder.imgView.setImageResource(R.drawable.icon_feeder);
			Date d = new Date((long) (fd.getBegin_time()*1000));
			String type = DictionaryDataManager.getInstance().getValueById(context, AssertTool.FEED_TYPE, fd.getFeeding_type());
			String vomits = fd.getVomits() == 1? "有吐奶":"无吐奶";
			holder.txtValue.setText(format.format(d) + "开始喝 "+type + fd.getAmount()+"ml 时长"+formatTime(fd.getDuration()) + vomits);
			long time = (long) baby.getBirthday() * 1000;
			String age = DateUtil.getBabyAge(time, (long)fd.getCreate_time() );
			holder.txtAge.setText(age);
			holder.txtDate.setText(DateUtil.getDateData(fd.getCreate_time()));
		} else if (nur instanceof BreastFeeding) {
			BreastFeeding bf = (BreastFeeding) nur;
			holder.imgView.setImageResource(R.drawable.icon_breast);
			String type = DictionaryDataManager.getInstance().getValueById(context, AssertTool.BREAST_FEED, bf.getBreast_type());
			Date d = new Date((long)bf.getBegin_time()*1000);
			String vomits = bf.getVomits() == 1?"有吐奶":"无吐奶";
			holder.txtValue.setText(format.format(d)+"开始喝母乳  "+type+" 时长 "+formatTime(bf.getDuration())+ " " + vomits);
			long time = (long) baby.getBirthday() * 1000;
			String age = DateUtil.getBabyAge(time, (long) bf.getCreate_time());
			holder.txtAge.setText(age);
			holder.txtDate.setText(DateUtil.getDateData(bf.getCreate_time()));
		}
		
		return convertView;
	}
	
	class ViewHolder {
		ImageView imgView;
		TextView txtAge;
		TextView txtValue;
		TextView txtDate;
	}
	
	private String formatTime(int duration){
		int h = duration / 60;
		int m = duration % 60;
		if (h != 0 && m != 0) {
			return h+"小时"+m+"分钟";
		} else {
			if (h == 0) {
				return m+"分钟";
			} else if (m == 0) {
				return h+"小时";
			} else {
				return "";
			}
		}
	}
}
